﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class AllResource : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int page;
        string strSQL , strPage;
        DataTable DTRes;
        if ( Request["page"] == null||Request["page"].ToString() == "" )
            page = 0;
        else
            page = Convert.ToInt32(Request["page"].ToString());

        MysqlHelp AllRes = new MysqlHelp();
        strSQL = makeSqlQue();
        strSQL = string.Format("{0} ORDER BY updatatime DESC", strSQL);
        DTRes = AllRes.GetDatabysql(strSQL);
        int iPageCount = DTRes.Rows.Count;
        
        page = page > iPageCount ? page-5: page;
        page = page < 0 ? page + 5 : page;

        strSQL = string.Format("{0} LIMIT {1},5",strSQL, page);
        DTRes = AllRes.GetDatabysql(strSQL);
        int iCount = DTRes.Rows.Count;
        int i = 0;
        Response.Write("<table width='100%' border='1'>");
        Response.Write("<tr>");
        Response.Write("<td width='5%'>" + "序号" + "</td>");
        Response.Write("<td width='60%'>" + "标题" + "</td>"); 
        Response.Write("<td width='10%'>" + "学科" + "</td>");
        Response.Write("<td width='10%'>" + "发布者" + "</td>");
        Response.Write("<td width='15%'>" + "更新时间" + "</td>");
        Response.Write("</tr>");
        while(i++<iCount)
        {
            Response.Write("<tr>");
            Response.Write("<td>"+(page+i).ToString()+"</td>");//序号
            Response.Write("<td><a href = 'View.aspx?id="+DTRes.Rows[i-1]["id"]+"'>" + DTRes.Rows[i - 1]["title"].ToString() + "</a></td>");//标题   
            Response.Write("<td>" + DTRes.Rows[i - 1]["subject"].ToString() + "</td>");//所属学科
            Response.Write("<td>" + DTRes.Rows[i - 1]["publisherID"].ToString() + "</td>");//发布者
            string strTime = Convert.ToDateTime(DTRes.Rows[i - 1]["updatatime"]).ToString("yyyy/MM/dd");
            Response.Write("<td>" + strTime + "</td>");//更新时间
            Response.Write("</tr>");
        } 
        Response.Write("</table>");

        string strPost = GetPostStr();
        strPage = string.Format("<a href='javascript:;' onclick=\"GetAllResource(0,'{0}')\">首页</a>\t", strPost);
        Response.Write(strPage);

        strPage = string.Format("<a href='javascript:;' onclick=\"GetAllResource({0},'{1}')\">上一页</a>\t", page - 5, strPost);
        Response.Write(strPage);

        strPage = string.Format("<a href='javascript:;' onclick=\"GetAllResource({0},'{1}')\">下一页</a>\t", page + 5, strPost);
        Response.Write(strPage);

        strPage = string.Format("<a href='javascript:;' onclick=\"GetAllResource({0},'{1}')\">末页</a>\t", iPageCount - iPageCount % 5, strPost);
        Response.Write(strPage);
    }
    protected string makeSqlQue()
    {
        string sql = "";
        //、精确查询，多条件查询
        for (int i = 0; i < 7; i++)
        {
            string str = "";
            switch (i)
            {
                case 0: str = "keyword"; break;
                case 1: str = "source"; break;
                case 2: str = "author"; break;
                case 3: str = "publisherID"; break;
                case 4: str = "subject"; break;
                case 5: str = "filetype"; break;
                case 6: str = "title"; break;
                    
            }
            var tmp = Request[str];
            if(tmp!=null)
            {
                if(str == "filetype")
                {
                    MysqlHelp dataprovider = new MysqlHelp();
                    string typeSql = "select * from filetype where filetype = '" + tmp.ToString() + "'";
                    DataTable recall1 = dataprovider.GetDatabysql(typeSql);
                    int typeCount = recall1.Rows.Count;
                    typeSql = "";
                    for(int j=0;j<typeCount;j++)
                    {
                        if (typeSql == "")
                        {
                            typeSql = "filetype in (" + recall1.Rows[j]["id"].ToString();
                        }
                        else typeSql += "," + recall1.Rows[j]["id"].ToString();
                    }
                    typeSql += ")";
                    if (sql == "")
                        sql = "SELECT * FROM resource WHERE " + typeSql;
                    else
                        sql += "AND " + typeSql;
                }
                else if (sql == "")
                {
                    sql = "SELECT * FROM resource WHERE "+str+" LIKE '%"+tmp.ToString()+"%'";
                }
                else sql += " AND " + str + " LIKE '%" + tmp.ToString() + "%'";
            }
            
        }
        ///精确查询中对时间的单独处理
        var tmp1 = Request["timefrom"];
        var tmp2 = Request["timeto"];
        if(tmp1!=null&&tmp2!=null)
        {
            if(sql == "")
            {
                sql = "SELECT * FROM resource WHERE updatatime BETWEEN '" + tmp1.ToString() + "' AND '" + tmp2.ToString() + "'";
            }
            else
                sql += "AND updatatime BETWEEN '" + tmp1.ToString() + "' AND '" + tmp2.ToString() + "'";
        }
        //普通搜索，多字段查询
        var tmp3 = Request["common"];
        if(tmp3 != null)
        {
            sql = "SELECT * FROM resource WHERE CONCAT(title,keyword,introduction) LIKE '%" + tmp3.ToString() + "%'";
        }
        //无条件搜索，用于显示全部资源
        if (sql == "")
            sql = "SELECT * FROM resource";
        return sql;
    }
    protected string GetPostStr()
    {
        string strPost = "";
        string strTmp="";
        for(int i=0;i<10;i++)
        {
            switch(i)
            {
                case 0: strTmp = "keyword"; break;
                case 1: strTmp = "source"; break;
                case 2: strTmp = "author"; break;
                case 3: strTmp = "publisherID"; break;
                case 4: strTmp = "subject"; break;
                case 5: strTmp = "filetype"; break;
                case 6: strTmp = "title"; break;
                case 7: strTmp = "timefrom"; break;
                case 8: strTmp = "timeto"; break;
                case 9: strTmp = "common"; break;
            }
            var tmp = Request[strTmp];
            if(tmp!=null)
            {
                if(strPost == "")
                    strPost = strTmp + "=" + tmp.ToString();
                else
                    strPost += "&" + strTmp + "=" + tmp.ToString();
            }
        }
        if (strPost == "")
            strPost = null;
        return strPost;
    }
}
